package com.woniuxy.charin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.charin.mapper.InvitationFriendMapper;
import com.woniuxy.charin.mapper.InvitationIncomeDetailMapper;
import com.woniuxy.charin.mapper.InvitationInfoMapper;
import com.woniuxy.charin.model.entity.InvitationFriend;
import com.woniuxy.charin.model.entity.InvitationIncomeDetail;
import com.woniuxy.charin.model.entity.InvitationInfo;
import com.woniuxy.charin.model.vo.InvitationDetailVO;
import com.woniuxy.charin.service.InvitationInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
* @author 28196
* @description 针对表【invitation_info】的数据库操作Service实现
* @createDate 2024-12-23 21:27:49
*/
@Service
@Slf4j
public class InvitationInfoServiceImpl extends ServiceImpl<InvitationInfoMapper, InvitationInfo>
        implements InvitationInfoService{
    @Autowired
    private InvitationFriendMapper friendMapper;

    @Autowired
    private InvitationIncomeDetailMapper incomeDetailMapper;


    @Override
    public InvitationDetailVO getInvitationDetail(Integer id) {
        InvitationDetailVO vo = new InvitationDetailVO();

        // 查询基本信息
        InvitationInfo info = this.getById(id);
        vo.setInvitationInfo(info);

        // 查询好友列表
        List<InvitationFriend> friends = getFriendsByInvitationId(id);
        vo.setFriendList(friends);

        // 查询收益明细
        List<InvitationIncomeDetail> incomes = getIncomeDetailsByInvitationId(id);
        vo.setIncomeDetails(incomes);

        // 设置统计信息
        vo.setTotalFriends(friends.size());
        vo.setTotalIncome(calculateTotalIncome(incomes));

        return vo;
    }

    @Override
    public List<InvitationFriend> getFriendsByInvitationId(Integer id) {
        return friendMapper.selectList(
                new QueryWrapper<InvitationFriend>()
                        .eq("invitation_id", id)
                        .orderByDesc("creation_time")
        );
    }

    @Override
    public List<InvitationIncomeDetail> getIncomeDetailsByInvitationId(Integer id) {
        return incomeDetailMapper.selectList(
                new QueryWrapper<InvitationIncomeDetail>()
                        .eq("invitation_id", id)
                        .orderByDesc("income_time")
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeInvitationWithRelations(Long id) {
        try {
            // 删除好友信息
            friendMapper.delete(new QueryWrapper<InvitationFriend>()
                    .eq("invitation_id", id));

            // 删除收益明细
            incomeDetailMapper.delete(new QueryWrapper<InvitationIncomeDetail>()
                    .eq("invitation_id", id));

            // 删除邀请信息
            return this.removeById(id);
        } catch (Exception e) {
            log.error("删除邀请信息及关联数据失败", e);
            throw new RuntimeException("删除失败", e);
        }
    }

    private BigDecimal calculateTotalIncome(List<InvitationIncomeDetail> incomes) {
        return incomes.stream()
                .map(InvitationIncomeDetail::getIncomeAmount)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }
}




